package hu.ikti.oamadmin.types.entities;

import hu.ikti.oamadmin.types.enums.Gender;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.Pattern;

import org.jboss.seam.annotations.Name;

@Entity
@Name("customer")
@Table(name = "CUSTOMER")
@SequenceGenerator(sequenceName = "CUSTOMER_SEQ", name = "SEQ", allocationSize = 1)
public class Customer implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private String firstName;
	private String lastName;
	private Date birthDate;
	private Gender gender;
	private List<Gateway> gateways;
	private Address address;
	private String pin;

	public Customer() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		try {
			birthDate = formatter.parse("1900/01/01");
		} catch (ParseException e) {
		}

	}

	public Customer(String firstName, String lastName, Date birthDate,
			Gender gender) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.birthDate = birthDate;
		this.gender = gender;
	}

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
	public Long getId() {
		return id;
	}

	@Column(name = "FIRST_NAME", length = 15, nullable = false)
	@Length(max = 15, message = "max length: 15")
	@NotEmpty(message = "not null")
	public String getFirstName() {
		return firstName;
	}

	@Column(name = "LAST_NAME", length = 15, nullable = false)
	@Length(max = 15, message = "max length: 15")
	@NotEmpty(message = "not null")
	public String getLastName() {
		return lastName;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "BIRTH_DATE", nullable = false)
	public Date getBirthDate() {
		return birthDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "GENDER", length = 1, nullable = false)
	public Gender getGender() {
		return gender;
	}

	@OneToMany
	@JoinTable(name = "CUSTOMER_GATEWAY_RT", joinColumns = { @JoinColumn(name = "CUSTOMER_FK") }, inverseJoinColumns = { @JoinColumn(name = "GATEWAY_FK") })
	public List<Gateway> getGateways() {
		return gateways;
	}

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "ADDRESS_FK")
	public Address getAddress() {
		return address;
	}

	@Column(name = "PIN", unique = true, nullable = false)
	@Pattern(regex = "[0-9]{6,6}[A-Z]{2,2}", message = "PIN format: 000000AA")
	public String getPin() {
		return pin;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public void setGateways(List<Gateway> gateways) {
		this.gateways = gateways;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public void setPin(String pin) {
		this.pin = pin;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((pin == null) ? 0 : pin.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Customer other = (Customer) obj;
		if (pin == null) {
			if (other.pin != null)
				return false;
		} else if (!pin.equals(other.pin))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + ", firstName=" + firstName
				+ ", lastName=" + lastName + ", birthDate=" + birthDate
				+ ", gender=" + gender + "]";
	}

}
